/* 
*   进行local数据存储管理的工具模块
*/

const USER_KEY = 'user_key';

interface IUser {
    username: string;
    password: string;
}
interface IUserData {
    store: Storage;
    saveUser: (user: IUser) => void;
    readUser: () => IUser | null;
    removeUser: () => void;
}

class UserData implements IUserData {
    store: Storage = localStorage;
    saveUser(user: IUser) {
        this.store.setItem(USER_KEY, JSON.stringify(user));
    }
    readUser(): IUser | null {
        const val: string | null = this.store.getItem(USER_KEY);
        return val && JSON.parse(val);
    }
    removeUser() {
        this.store.removeItem(USER_KEY);
    }
}

const userData: UserData = new UserData();

export default userData;